package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.Banner;
import com.burgeon.backend.SHOPBACKEND.entity.ClassifyItem;
import com.burgeon.backend.SHOPBACKEND.entity.IndexImg;
import com.burgeon.backend.SHOPBACKEND.entity.Seckill;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendIndexImgMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.GoodsClassifyItemMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackentIndexImgService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import com.burgeon.backend.SHOPBACKEND.service.ex.UploadException;
import com.burgeon.backend.Util.BannerLink;
import com.burgeon.backend.mappers.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

@Service
public class BackentIndexImgServiceImpl implements BackentIndexImgService {

    @Autowired
    private BackendGoodsMapper backendGoodsMapper;
    @Autowired
    private BackendIndexImgMapper backendImgMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private GoodsClassifyItemMapper goodsClassifyItemMapper;

    /******上传轮播图*****/
    @Override
    public void addIndexBanner(MultipartFile file, String account, String type, String message) throws UploadException {
        String link = "null";
        if ("P".equals(type)) {
            //款号查找id  为空抛出异常
            String id = backendGoodsMapper.getGoodsIdByCode(message,account);
            if (null == id){
                throw new UploadException("商品不存在");
            }
            link = BannerLink.getBannerGoodsLink(id);
        } else if("Q".equals(type)){
            link = BannerLink.getBannerLink(message);
        } else if ("L".equals(type)){//L对应分类
            ClassifyItem classifyItem = goodsClassifyItemMapper.getClassItemById(message);
            link = BannerLink.getBannerClassityItemLink(classifyItem.getClassifyId(),classifyItem.getId());
        }else if("B".equals(type)){
            link = BannerLink.getBrandLink(message);
        }

        String types = "banner";//将图片存到本地仓库
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        //不为空删除之前的,根据商品id
        //backendImgMapper.deIndexBannerByGid(id);

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        fileName = message +"-"+ type + suffixName; // 新文件名
        //存入数据库
        String token = accountMapper.GetTokenForAccount(account);//获取token
        Banner banner = new Banner();
        banner.setUrl("http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/" + fileName);
        //banner.setGid(id);
        banner.setToken(token);
        banner.setPageLink(link);
        banner.setAccount(account);
        backendImgMapper.addIndexBanner(banner);
        saveFile(file, fileName, filePath);

    }

    //根据id删除首页轮播图
    @Override
    public void deIndexBannerById(String id) throws ErrorDataException {
        try {
            backendImgMapper.deIndexBannerById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException("删除类型错误");
        }
    }

    @Override
    public List<Banner> findIndexBanner(String account) {

        List<Banner> data = backendImgMapper.findIndexBanner(account);
        Random random = new Random();
        for (Banner banner : data){
            banner.setUrl(banner.getUrl().replace("lfwdcdn.cloudsmicro.com","lfwd.cloudsmicro.com:8080")+"#"+random.nextInt() * 100000 );
        }
        return data;
    }

    /******************************积分商城的图,新品上市，活动专区********************************/

    /**
     * 新品上市图
     */
    @Override
    public Integer upIndexNew(MultipartFile file, String account) throws UploadException {
        String types = "index";     //将图片存到本地仓库
        //String id = backendGoodsMapper.getGoodsIdByCode(code,account);
//        if (null == id){
//            throw new UploadException("商品不存在");
//        }
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        fileName = "i"+ suffixName; // 新文件名

        //存入数据库
        String imgI = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/" + fileName;

        IndexImg indexImg = backendImgMapper.getIndexImg(account);//查询是否存在account的indexImg
        String token = accountMapper.GetTokenForAccount(account);//获取token
        if (indexImg == null) {
            IndexImg indexImg1 = new IndexImg();
            indexImg1.setAccount(account);
            indexImg1.setToken(token);
            indexImg1.setImgI(imgI);
            //indexImg1.setIid(id);
            backendImgMapper.addIndexNewImg(indexImg1);
        }
        backendImgMapper.upIndexNew(imgI, account);
        saveFile(file, fileName, filePath);
        return null;
    }

    //活动专区图
    @Override
    public Integer upIndexActivity(MultipartFile file, String account) throws UploadException {
        String types = "index";     //将图片存到本地仓库
        //String id = backendGoodsMapper.getGoodsIdByCode(account);
//        if (null == id){
//            throw new UploadException("商品不存在");
//        }
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名

        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        fileName = "p" + suffixName; // 新文件名

        //存入数据库
        String imgP = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/p" + suffixName;

        IndexImg indexImg = backendImgMapper.getIndexImg(account);//查询是否存在account的indexImg
        String token = accountMapper.GetTokenForAccount(account);//获取token
        if (indexImg == null) {
            IndexImg indexImg1 = new IndexImg();
            indexImg1.setAccount(account);
            indexImg1.setToken(token);
            indexImg1.setImgP(imgP);
            //indexImg1.setPid(id);
            backendImgMapper.addIndexActivityImg(indexImg1);
        }
        backendImgMapper.upIndexActivity(imgP,account);
        saveFile(file, fileName, filePath);
        return null;
    }


    /**
     * 会员专享
     **/
    @Override
    public Integer upHyzx(MultipartFile file, String account) throws UploadException {
        System.err.println(file.getOriginalFilename());
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        String types = "index";     //将图片存到本地仓库

        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        int i=(int)(Math.random()*100);
        fileName = "hyzx"+i + suffixName; // 新文件名

        //存入数据库
        String hyzx = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/" + fileName;

        IndexImg indexImg = backendImgMapper.getIndexImg(account);//查询是否存在account的indexImg
        String token = accountMapper.GetTokenForAccount(account);//获取token
        //不存在就执行新增方法
        if (indexImg == null) {
            IndexImg indexImg1 = new IndexImg();
            indexImg1.setAccount(account);
            indexImg1.setToken(token);
            indexImg1.setHyzx(hyzx);
            backendImgMapper.addIndexHyzxImg(indexImg1);
        }
        //有数据就执行修改方法
        backendImgMapper.upHyzx(hyzx, account);
        saveFile(file, fileName, filePath);
        return null;
    }

    /**
     * 积分生活
     **/
    @Override
    public Integer upJfsh(MultipartFile file, String account) throws UploadException {
        String types = "index";     //将图片存到本地仓库

        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        int i=(int)(Math.random()*100);
        fileName = "jfsh" +i+ suffixName; // 新文件名

        //存入数据库
        String jfsh = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/" + fileName;

        IndexImg indexImg = backendImgMapper.getIndexImg(account);//查询是否存在account的indexImg
        String token = accountMapper.GetTokenForAccount(account);//获取token
        //不存在就执行新增方法
        if (indexImg == null) {
            IndexImg indexImg1 = new IndexImg();
            indexImg1.setAccount(account);
            indexImg1.setToken(token);
            indexImg1.setJfsh(jfsh);
            backendImgMapper.addIndexJfshImg(indexImg1);
        }
        //有数据就执行修改方法
        backendImgMapper.upJfsh(jfsh, account);
        saveFile(file, fileName, filePath);
        return null;
    }

    /**
     * 积分换购
     **/
    @Override
    public Integer upJfhg(MultipartFile file, String account) throws UploadException {
        String types = "index";     //将图片存到本地仓库
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        int i=(int)(Math.random()*100);
        fileName = "jfhg"+i + suffixName; // 新文件名

        //存入数据库
        String jfhg = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/" + fileName;

        IndexImg indexImg = backendImgMapper.getIndexImg(account);//查询是否存在account的indexImg
        String token = accountMapper.GetTokenForAccount(account);//获取token
        //不存在就执行新增方法
        if (indexImg == null) {
            IndexImg indexImg1 = new IndexImg();
            indexImg1.setAccount(account);
            indexImg1.setToken(token);
            indexImg1.setJfhg(jfhg);
            backendImgMapper.addIndexJfhgImg(indexImg1);
        }
        //有数据就执行修改方法
        backendImgMapper.upJfhg(jfhg, account);
        saveFile(file, fileName, filePath);
        return null;
    }

    /***积分礼券***/
    @Override
    public Integer upJflq(MultipartFile file, String account) throws UploadException {
        String types = "index";     //将图片存到本地仓库
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        int i=(int)(Math.random()*100);
        fileName = "jflq"+i + suffixName; // 新文件名

        //存入数据库
        String jflq = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/" + fileName;

        IndexImg indexImg = backendImgMapper.getIndexImg(account);//查询是否存在account的indexImg
        String token = accountMapper.GetTokenForAccount(account);//获取token
        //不存在就执行新增方法
        if (indexImg == null) {
            IndexImg indexImg1 = new IndexImg();
            indexImg1.setAccount(account);
            indexImg1.setToken(token);
            indexImg1.setJflq(jflq);
            backendImgMapper.addIndexJflqImg(indexImg1);
        }
        //有数据就执行修改方法
        backendImgMapper.upJflq(jflq, account);

        saveFile(file, fileName, filePath);
        return null;
    }

    /**
     * 查看新品上市
     */
    @Override
    public IndexImg findIndexNew(String account) {
        return backendImgMapper.findIndexNew(account);
    }

    /**
     * 查看活动专区
     */
    @Override
    public IndexImg findIndexActivity(String account) {
        return backendImgMapper.findIndexActivity(account);
    }

    /**
     * 查看积分商城
     */
    @Override
    public IndexImg findIndexJf(String account) {
        return backendImgMapper.findIndexJf(account);
    }

    /***************************秒杀*********************************/
    @Override
    @Transactional
    public Integer addIndexSkill(MultipartFile file, String account) throws UploadException {
        String types = "seckill";     //将图片存到本地仓库
        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new UploadException("文件为空");
        }

        //不为空删除之前的,根据商品id
        backendImgMapper.deIndexSeckill(account);

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名
        //http://lfwdcdn.cloudsmicro.com/img/mgg-shop/2.png
        String filePath = "//root//code//img//" + account + "-shop//" + types + "//"; // 上传后的路径
        fileName = types + suffixName; // 新文件名
        //存入数据库
        String token = accountMapper.GetTokenForAccount(account);//获取token

        //SimpleDateFormat ssdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd" + " " + "HH:mm:ss");
        try {

                 //当前的时间
            Seckill seckill = new Seckill();
            seckill.setAccount(account);
            seckill.setImg("http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/" + types + "/" + types + suffixName);

            seckill.setToken(token);

            backendImgMapper.addIndexSkill(seckill);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UploadException("出现错误");
        }

        saveFile(file, fileName, filePath);
        return null;
    }

    //查看秒杀
    @Override
    public Seckill findIndexSeckill(String account) {
        return backendImgMapper.findIndexSeckill(account);
    }

    //保存文件
    private void saveFile(MultipartFile multipartFile, String fileName, String filePath) {
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            multipartFile.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            throw new com.burgeon.backend.service.ex.UploadException("系统错误");
        } catch (Exception e) {
            System.err.println(e.getMessage());
            throw new com.burgeon.backend.service.ex.UploadException("系统错误");
        }
    }



}
